// FbmlService.cs
// Facebook/Framework/Service
// Copyright (c) 2007, Nikhil Kothari. All Rights Reserved.
//

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using Facebook.Service.Core;

namespace Facebook.Service {

    /// <summary>
    /// Represents the service APIs that allow working against FBML.
    /// </summary>
    public sealed class FbmlService {

        private FacebookSession _session;

        internal FbmlService(FacebookSession session) {
            Debug.Assert(session != null);
            _session = session;
        }

        /// <summary>
        /// Updates the FBML markup associated with a named handle.
        /// A named handle can then be used in FBML with the &lt;fb:ref handle="..." /&gt;
        /// construct.
        /// </summary>
        /// <param name="handleName">The name of the handle.</param>
        /// <param name="fbmlMarkup">The markup associated with the handle.</param>
        /// <returns>true if successful; false otherwise.</returns>
        public bool RefreshContentHandle(string handleName, string fbmlMarkup) {
            FacebookRequest request = new FacebookRequest(_session);
            request.Parameters["handle"] = handleName;
            request.Parameters["fbml"] = fbmlMarkup;

            FacebookResponse response = request.InvokeMethod("fbml.setRefHandle");
            if (response.Status == FacebookResponseStatus.Succeeded) {
                return FacebookResponse.CoerceBoolean(response.Result);
            }

            return false;
        }

        /// <summary>
        /// Updates the FBML markup cached for the specified URL. The FBML returned
        /// by the specified callback URL can be used with the &lt;fb:ref url="..." /&gt;
        /// construct.
        /// </summary>
        /// <param name="url">The url that is to be refreshed.</param>
        /// <returns>true if successful; false otherwise.</returns>
        public bool RefreshContentLink(string url) {
            FacebookRequest request = new FacebookRequest(_session);
            request.Parameters["url"] = url;

            FacebookResponse response = request.InvokeMethod("fbml.refreshRefUrl");
            if (response.Status == FacebookResponseStatus.Succeeded) {
                return FacebookResponse.CoerceBoolean(response.Result);
            }

            return false;
        }

        /// <summary>
        /// Updates the image cached for the specified URL.
        /// </summary>
        /// <param name="url"></param>
        /// <returns>true if successful; false otherwise.</returns>
        public bool RefreshReferencedImage(string url) {
            FacebookRequest request = new FacebookRequest(_session);
            request.Parameters["url"] = url;

            FacebookResponse response = request.InvokeMethod("fbml.refreshImgSrc");
            if (response.Status == FacebookResponseStatus.Succeeded) {
                return FacebookResponse.CoerceBoolean(response.Result);
            }

            return false;
        }
    }
}
